﻿using System;
using System.Web.UI;
using System.IO;
namespace BaseSystem
{
    public partial class errorpage : System.Web.UI.Page
    {
        protected string ErrorMessage;
        protected void Page_Load(object sender, EventArgs e)
        {
            //ErrMsg.Text = Application["error"].ToString();
            ErrMsg.Text=Request.QueryString["Msg"].ToString();

            ErrorMessage=Request.QueryString["Msg"].ToString();
            //WriteErrorLog(ErrorMessage);
        }
        private void WriteErrorLog(string errormessage)
        {
            
            //StreamWriter sw = new StreamWriter( @"d:\ErrorLog.txt");
            
            //sw.WriteLine("-------------");
            //sw.WriteLine(errormessage);
            //sw.WriteLine("-------------");
            //sw.Flush();
            //sw.Close();
            Exception objErr = Server.GetLastError().GetBaseException();
            string error = string.Empty;
            string errortime = string.Empty;
            string erroraddr = string.Empty;
            string errorinfo = string.Empty;
            string errorsource = string.Empty;
            string errortrace = string.Empty;

            error += "ErrorTime:" + System.DateTime.Now.ToString() + "<br>";
            errortime = "ErrorTime:" + System.DateTime.Now.ToString();

            error += "ErrorPage: " + Request.Url.ToString() + "<br>";
            erroraddr = "ErrorPage: " + Request.Url.ToString();

            error += "ErrorMessage: " + objErr.Message + "<br>";
            errorinfo = "ErrorMessage: " + objErr.Message;

            //error +="ErrorSource:"+objErr.Source+"<br>";
            //error += "ErrorStackTrace:" + objErr.StackTrace + "<br>";
            errorsource = "ErrorSource:" + objErr.Source;
            errortrace = "ErrorStackTrace:" + objErr.StackTrace;
            error += "--------------------------------------<br>";
            Server.ClearError();
            Application["error"] = error;

            //a file must be written by one process
            System.IO.StreamWriter writer = null;
            try
            {
                lock (this)
                {
                    // write to log
                    string year = DateTime.Now.Year.ToString();
                    string month = DateTime.Now.Month.ToString();
                    string path = string.Empty;
                    string filename = DateTime.Now.Day.ToString() + ".txt";
                    path = Server.MapPath("~/Error/") + year + "/" + month;
                    //if directory not exists,create it.
                    if (!System.IO.Directory.Exists(path))
                    {
                        System.IO.Directory.CreateDirectory(path);
                    }
                    System.IO.FileInfo file = new System.IO.FileInfo(path + "/" + filename);
                    //if (!file.Exists)
                    //    file.Create();
                    //file.Open(System.IO.FileMode.Append);        
                    writer = new System.IO.StreamWriter(file.FullName, true);//if file not exists create it,true  stand for append
                    writer.WriteLine("User IP:" + Request.UserHostAddress);
                    //if (Session["UserName"] != null)
                    //{
                    //    writer.WriteLine("User Name" + System.Web.HttpContext.Current.Session["UserName"].ToString());
                    //}
                    writer.WriteLine(errortime);
                    writer.WriteLine(erroraddr);
                    writer.WriteLine(errorinfo);
                    writer.WriteLine(errorsource);
                    writer.WriteLine(errortrace);
                    writer.WriteLine("--------------------------------------------------------------------------------------");
                    //writer.Close();
                }
            }
            finally
            {
                if (writer != null)
                    writer.Close();

            }
            Response.Redirect("~/Error/ErrorPage.aspx");


        }
        




    }

}